// use ferris_says::say; // from the previous step
// use std::io;
// use rand::Rng;
// use std::cmp::Ordering;

mod advance;
mod grep;
mod playground;
mod primary;
mod sort_size;
mod util;
mod echo_server;
mod Test;

use advance::{aes_demo, futures_test, http, intelli_ptr, M3u8Item};
use std::fmt::format;
use util::{
    hash_map, iter_closures, log, math::{self, secr::sum_multi as deep_sm}, panic_demo, schedule_test, str_demo, thread, traits, vec_demo
};

pub fn main() {
    // util::thread_pool::run();
    // http::main();
    // futures_test::run();
    // echo_server::main();
    // data_type();
    // let x = 13;
    // println!("fn_hello() return {}", fn_hello(x,12));

    // condition();
    // loop_fn();
    // recv(13);

    // util::embed::main();
    // http::main();
    // primary::main();
    // sort_size::main();
    // log::main();

    // mod_test();
    // vec_demo::main();
    // str_demo::main();
    // hash_map::main();
    // panic_demo::main();
    // traits::main();

    // let args:Vec<String> = std::env::args().collect();
    // grep::run(args);
    // let args:Vec<String> = std::env::args().collect();
    // grep::run(args);

    thread::run();
    // iter_closures::run();

    // intelli_ptr::run();
    // aes2::main();
    // intelli_ptr::run();
    // aes_demo::main();
    // http::main();
    // M3u8Item::main();

    // playground::main();
}
#[cfg(test)]
mod tests {
    use std::time::{Duration, Instant};

    use chrono::{Datelike, Local, Timelike};

    #[test]
    #[should_panic]
    fn t1() {
        println!("test1.==");
        assert_eq!(2 + 2, 4);
        assert!(4 > 6, "4小于6{}", "..");
    }
    #[test]
    fn t2() -> Result<(), String> {
        if 2 * 3 < 4 {
            Ok(())
        } else {
            Err("发生了错误 2*3 》 4".to_string())
        }
    }
    /**
     * 定时器测试
     */
    #[test]
    fn timer_test() {
        use job_scheduler_ng::{Job, JobScheduler};

        let mut sche = JobScheduler::new();
        sche.add(Job::new("0/3 * * * * *".parse().unwrap(), || {
            println!("1====i will exec every 3seconds, now:{:?}", Instant::now());
        }));
        sche.add(Job::new("0/5 * * * * *".parse().unwrap(), || {
            println!("2**** will exec every 5seconds, now:{:?}", Instant::now());
        }));

        loop {
            //到达指定时间之前会一直阻塞
            sche.tick();
        }

        //tokio 定时器
        // let mut intv = tokio::time::interval(Duration::from_secs(3));
        // let t1 = intv.tick().await;
        // let t2 = intv.tick().await;
    }

    #[test]
    fn Shift_operation() {
        let x: u8 = 0x80;
        let y = x >> 4;
        println!("y:{} b:{:b}", y, y);
    }

    #[test]
    fn test_localtime(){
        //chrono 日期时间库
        let localtime = Local::now();

        let year = localtime.year();
        let month = localtime.month();
        let day = localtime.day();

        println!("localtime={:?}", localtime);
        println!("year={}, month={}, day={}", year, month, day);
        println!("hour={}, minius={}, seconds={}", localtime.hour(), localtime.minute(), localtime.second());

        println!("localtime format: {}", localtime.format("fmt : %Y-%m-%d %H:%M:%S %z %Z"));
    }
}

fn mod_test() {
    println!("x^y={}", math::pow(2, 3));
    println!("(x+y)^2={}", math::sum_multi_br(2, 3));
    println!("x^y={}", deep_sm(2, 3));
    println!("x+y={}", math::secr::sum(2, 3));
}

fn recv(num: i32) {
    if num > 1 {
        recv(num - 1);
    }
    println!("{}", num);
}
fn loop_fn() {
    // loop 相当于 while(true)
    let mut num = 0;
    loop {
        num += 1;
        if num > 100 {
            break;
        }
        println!("num = {}", num);
    }
    // loop 作为表达式
    let res = loop {
        num -= 1;
        if num < 5 {
            break num * 2;
        }
    };
    println!("loop result is {}", res);
    // while 循环
    let mut res = 0;
    while res < 100 {
        println!("while: res= {}", res);
        res += 1;
    }
    //for 循环
    let arr = [3, 9, 7, 10, 19];
    println!("arr len is {}", arr.len());
    for ele in arr.iter() {
        println!("value is {}", ele);
    }
    // 这段代码会报错
    // let mut idx=0;
    // while idx < arr.len()+1 {
    //     println!("arr[{}] is {}",idx,arr[idx]);
    //     idx += 1;
    // }
    // rev type
    for ele in (1..11).rev() {
        println!("() value is {}", ele)
    }
}

fn condition() {
    let num = 9;
    if num > 5 {
        println!("num is greater than 5!");
    } else if num > 10 {
        println!("num is greater than 10!");
    } else {
        println!("num in else.");
    }

    let num = 8;
    let res = if num == 9 {
        0
    } else if num > 9 {
        1
    } else {
        num + 1
    };
    println!("res= {}", res);
}

fn fn_hello(x: i32, y: i32) -> i32 {
    println!("x+y = {}", x + y);
    // return x + y-1;
    x + y - 1
}
fn data_type() {
    let s: String = "haha".to_string();
    let num: u16 = 0b10;
    println!("s={}, num= {}", s, num);

    let sum = 1 + 2;
    let multi = sum * 3;
    println!("sum={}, multi={}", sum, multi);

    let t = false;
    println!("t={}", t);

    let ch = 'Z';
    let ch2 = '好';
    println!("ch={}, ch2={}", ch, ch2);

    // 元组
    let mut tup = (1.2, 10, 'Z');
    let x = tup.1;
    tup.0 = 2.4;
    println!("tup={}, x={}, tup.0={}", tup.2, x, tup.0);

    // 数组
    let arr = [1, 2, 3];
    let arr2 = [3; 5];
    const NUM2: usize = 3;
    let arr3: [i32; NUM2] = [1, 2, 3];
    println!("arr[1]={}, arr2[4]={}", arr[1], arr2[4]);
}
